package fragment;

import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.customview.R;

import java.io.File;
import java.util.List;

import adapter.FileManagerVideoListAdapter;
import moudle.FileManagerMoudle;

/**
 * Created by eyesee on 2017/4/22.
 */

public class MenuFragmentTwo extends DialogFragment implements View.OnClickListener {

    private Dialog mDialog;
    private View mView;
    private FilemanagerFileList mFileList;
    private FileManagerVideoList mVideoList;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = new Dialog(getActivity());

        mView = View.inflate(getActivity(), R.layout.menufragment_view_two, null);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        mDialog.setContentView(mView);
        mDialog.setCanceledOnTouchOutside(true);

        //动画
        TranslateAnimation animation = new TranslateAnimation(-mView.getMeasuredWidth(), 0, 0, 0);
        animation.setDuration(1000);
        mView.setAnimation(animation);
        animation.toString();

        init();
        Window window = mDialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.LEFT;
        params.width = 400;
        params.height= 1000;
        window.setAttributes(params);
        return mDialog;
    }

    /**初始化*/
    private void init() {
        Button nameSort = (Button) mView.findViewById(R.id.namesort);
        Button timeSort = (Button) mView.findViewById(R.id.timesort);

        mFileList = new FilemanagerFileList();
        mVideoList = new FileManagerVideoList();


        /**视频列表,传入回调方法*/
        mVideoList.SetSortListener(new FileManagerVideoList.SortListener() {
            @Override
            public void TimeSort(ListView listView, FileManagerMoudle fileManagerMoudle, File file) {
                Toast.makeText(getActivity(), "videolist+时间排序", Toast.LENGTH_SHORT).show();
                List<File> timeSort = fileManagerMoudle.getTimeSort(file);
                FileManagerVideoListAdapter adapter = new FileManagerVideoListAdapter(getActivity(), timeSort);
                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void NameSort(ListView listView , FileManagerMoudle fileManagerMoudle, File file) {
                Toast.makeText(getActivity(), "videolist+名称排序", Toast.LENGTH_SHORT).show();
                if (file!=null){
                    List<File> nameSortList = fileManagerMoudle.getNameSort(file);
                    FileManagerVideoListAdapter adapter = new FileManagerVideoListAdapter(getActivity(), nameSortList);
                    listView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }else {
                    Toast.makeText(getActivity(), "file为空", Toast.LENGTH_SHORT).show();
                }
            }
        });

        nameSort.setOnClickListener(this);
        timeSort.setOnClickListener(this);

    }

    /**按钮点击*/
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.namesort:
                mVideoList.NameSortListener();  //执行名称排序回调方法
                mDialog.dismiss();
                break;
            case R.id.timesort:
                mVideoList.TimeSortListener();  //执行时间排序回调方法
                mDialog.dismiss();
                break;
        }
    }
}
